// TestDataEditDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GetFileVerInfo.h"
#include "TestDataEditDlg.h"
#include "InsertDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;

/////////////////////////////////////////////////////////////////////////////
// CTestDataEditDlg dialog


CTestDataEditDlg::CTestDataEditDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDataEditDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDataEditDlg)
	//}}AFX_DATA_INIT
}


void CTestDataEditDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDataEditDlg)
	DDX_Control(pDX, IDC_LIST_TESTDATA, m_listTestData);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTestDataEditDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDataEditDlg)
	ON_BN_CLICKED(IDC_BTN_NEW, OnBtnNew)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_TESTDATA, OnDblclkListTestdata)
	ON_BN_CLICKED(IDC_BTN_SAVETOFILE, OnBtnSavetofile)
	ON_BN_CLICKED(IDC_BTN_OPENTESTFILE, OnBtnOpentestfile)
	ON_BN_CLICKED(IDC_BTN_STARTTEST, OnBtnStarttest)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDataEditDlg message handlers

void CTestDataEditDlg::OnBtnNew() 
{
	// TODO: Add your control notification handler code here
	CFileDialog *pdlg = NULL;
	CFile file;
	CFileException fe;
	pdlg = new CFileDialog(FALSE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "TXT Files (*.TXT)|*.txt|All Files (*.*)|*.*||", NULL);
	if (NULL != pdlg)
	{
		if (IDOK == pdlg->DoModal())
		{
			CString strFileName = pdlg->GetPathName();
			
			if(!file.Open(strFileName, CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive, &fe))
			{
				MessageBox("Open File Error");
				return;
			}
			m_vTestData.clear();
			file.Close();
		}
		delete pdlg;
	}
}


BOOL CTestDataEditDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_listTestData.ModifyStyle(0, LVS_REPORT);
	CRect rc;
	m_listTestData.GetClientRect(&rc);
	m_listTestData.InsertColumn(0, _T("FileName"), LVCFMT_LEFT, rc.Width()/2);
	m_listTestData.InsertColumn(1, _T("FileVersion"), LVCFMT_LEFT, rc.Width()/2);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CTestDataEditDlg::OnDblclkListTestdata(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;

	int index = m_listTestData.GetSelectionMark();
	if (-1 == index)
	{
		CInsertDlg dlg;
		if (IDOK == dlg.DoModal())
		{
			TESTDATA testdata;
			testdata.strFileName = dlg.m_strFileName;
			testdata.strVerInfo = dlg.m_strFileVersion;
			m_vTestData.push_back(testdata);
			UpdateListCtrl();
		}
	}
}

void CTestDataEditDlg::UpdateListCtrl()
{
	m_listTestData.DeleteAllItems();
	LVITEM lvi;
	CString strItem;
	lvi.mask =  LVIF_TEXT;
	
	for (int i = 0; i < m_vTestData.size(); ++i)
	{
		strItem = m_vTestData[i].strFileName;
		lvi.iItem = i;
		lvi.iSubItem = 0;
		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
		m_listTestData.InsertItem(&lvi);

		strItem = m_vTestData[i].strVerInfo;
		lvi.iSubItem =1;
		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
		m_listTestData.SetItem(&lvi);
	}
}

void CTestDataEditDlg::OnBtnSavetofile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog *pdlg = NULL;
	pdlg = new CFileDialog(FALSE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||", NULL);
	if (NULL != pdlg)
	{
		if (IDOK == pdlg->DoModal())
		{
			HANDLE hFile = CreateFile(pdlg->GetPathName(),
				GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
				NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
			
			if (hFile == INVALID_HANDLE_VALUE)
				AfxMessageBox(_T("Couldn't create the file!"));
			else
			{
				// Attach a CFile object to the handle we have.
				CString str_filedata = "<testdata>\r\n", str_temp1, str_temp2;
				for(int i = 0; i < m_vTestData.size(); ++i)
				{
					str_temp1 = m_vTestData[i].strFileName + "\t" + m_vTestData[i].strVerInfo + "\r\n";
					str_filedata += str_temp1;
				}
				str_filedata += "</testdata>";
				char* cp_data = str_filedata.GetBuffer(str_filedata.GetLength());
				CFile myFile((int)hFile);
				
				int lenght = myFile.GetLength();
				
				// write string, without null-terminator
				myFile.SeekToEnd();
				myFile.Write(cp_data, str_filedata.GetLength());
				
				lenght = myFile.GetLength();
				
				myFile.Close();
			}
		}
		delete pdlg;
	}
}

void CTestDataEditDlg::OnBtnOpentestfile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog *pdlg = NULL;
	pdlg = new CFileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||", NULL);
	if (NULL != pdlg)
	{
		if (IDOK == pdlg->DoModal())
		{
			OpenTestDataFile(pdlg->GetPathName());
		}
		delete pdlg;
	}
}

void CTestDataEditDlg::OpenTestDataFile(CString str_filename)
{
	HANDLE hFile = CreateFile(str_filename,
		GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	
	if (hFile == INVALID_HANDLE_VALUE)
		AfxMessageBox(_T("Couldn't create the file!"));
	else
	{
		CFile myFile((int)hFile);
		int length = myFile.GetLength();
		
		
		//int i = length / 1024;
		char* sz_data = new char[length + 1];
		myFile.Read(sz_data, length);
		sz_data[length] = '\0';
		CString str_data(sz_data);

		int len = str_data.GetLength();
		int temp = str_data.Find("<testdata>", 0);
		str_data = str_data.Right(len - temp - 12);
		temp = str_data.Find("</testdata>", 0);
		str_data = str_data.Left(temp);

		m_vTestData.clear();
		while (-1 != str_data.Find("\r\n"))
		{
			TESTDATA testdata;
			temp = str_data.Find("\r\n");
			len = str_data.GetLength();
			CString str_testdata = str_data.Left(temp);
			str_data = str_data.Right(len - temp - 2);
			
			temp = str_testdata.Find("\t");
			len = str_testdata.GetLength();
			testdata.strFileName = str_testdata.Left(temp);
			testdata.strVerInfo = str_testdata.Right(len - temp - 1);

			m_vTestData.push_back(testdata);

		}
		
		UpdateListCtrl();

		delete[] sz_data;
		
		myFile.Close();
	}
}

void CTestDataEditDlg::OnBtnStarttest() 
{
	// TODO: Add your control notification handler code here
	if (0 != m_vTestData.size())
	{
		LVITEM lvi;
		CString strItem;
		lvi.mask =  LVIF_TEXT;

		m_listTestData.DeleteAllItems();
		m_listTestData.DeleteColumn(1);
		m_listTestData.DeleteColumn(0);

		CRect rc;
		m_listTestData.GetClientRect(&rc);
		m_listTestData.InsertColumn(0, _T("FileName"), LVCFMT_LEFT, rc.Width()/3);
		m_listTestData.InsertColumn(1, _T("FileVersion"), LVCFMT_LEFT, rc.Width()/3);
		m_listTestData.InsertColumn(2, _T("TestResult"), LVCFMT_LEFT, rc.Width()/3);
		for (int i = 0; i < m_vTestData.size(); ++i)
		{
			strItem = m_vTestData[i].strFileName;
			lvi.iItem = i;
			lvi.iSubItem = 0;
			lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
			m_listTestData.InsertItem(&lvi);
			
			strItem = m_vTestData[i].strVerInfo;
			lvi.iSubItem =1;
			lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
			m_listTestData.SetItem(&lvi);

			strItem = GetVersionFromPath(m_vTestData[i].strFileName);
			lvi.iSubItem =2;
			lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
			m_listTestData.SetItem(&lvi);

		}
	}
}

CString CTestDataEditDlg::GetVersionFromPath(CString IDEPath)
{
	DWORD dwLen = 0;
	char* lpData = NULL;
	
	dwLen = GetFileVersionInfoSize(IDEPath , 0);
	if (0 == dwLen)
	{
		return _T("");
	}
	lpData = new char[dwLen + 1];
	if (NULL != lpData)
	{
		if (!GetFileVersionInfo(IDEPath, 0, dwLen, lpData))
		{
			delete[] lpData;
			return _T("");
		}
		LPVOID lpBuffer = NULL;
		UINT uLen = 0;
		
		BOOL bSuccess = VerQueryValue(lpData, _T("\\StringFileInfo\\040904b0\\FileVersion"), &lpBuffer, &uLen);
		if (!bSuccess)
		{
			delete lpData;
			return CString("");
		}
		CString Ret = (char*)lpBuffer;
		Ret.Replace(',','.');
		Ret.Replace(' ', '0');
		delete[] lpData;
		return Ret;
	}
	else
	{
		return _T("");
	}
	//
}